package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;

	/**
	 *  
	 * @author PC-100
	 * 
	 */	
	public class PoolGameDemo extends Sprite
	{
		public function PoolGameDemo()
		{
			trace("PoolGameDemo");
			
			init();
		}
		
		/**
		 *  球杆
		 */	
		private var cue:Cue;
		/**
		 *  白球
		 */	
		private var cueBall:CueBall;
		/**
		 * 白球速度 
		 */		
		private var vx:Number = 5;
		private var vy:Number = 5;
		/**
		 * 球杆速度 
		 */		
		private var vxCue:Number = 0;
		private var vyCue:Number = 0;
		/**
		 * 摩擦力系数 
		 */		
		private var friction:Number = 0.95;
		/**
		 * 白球半径 
		 */		
		private const CUE_BALL_RADIUS:Number = 16;
		/**
		 * 球杆宽度 
		 */		
		private const CUE_WIDTH:Number = 150;
		
		/**
		 * 是否正在击球 
		 */		
		private var isHitting:Boolean = false;
		
		private function init():void
		{
			// 初始化球杆
			cue = new Cue();
			this.addChild( cue );
			//初始化白球
			cueBall = new CueBall();
			this.addChild( cueBall );
			
			cueBall.x = 200;
			cueBall.y = 200;
			
			this.addEventListener(Event.ENTER_FRAME, update);
			// 监听舞台点击事件，点击时出杆
			stage.addEventListener(MouseEvent.CLICK, onStageClick);
		}	
		/**
		 *  舞台点击事件处理
		 * @param event
		 * 
		 */		
		protected function onStageClick(event:MouseEvent):void
		{
			// 如果球杆隐藏中,则不能出杆
			if( !cue.visible ) return; 
			// 正在击球
			isHitting = true;
			// 球杆运动速度
			vxCue = ( cueBall.x - cue.x ) * 0.25;
			vyCue = ( cueBall.y - cue.y ) * 0.25;
		}
		/**
		 * 出杆操作 
		 * 
		 */		
		private function moveCue():void
		{
			cue.x += vxCue;
			cue.y += vyCue;
		}
		/**
		 * 
		 * @param event
		 * 
		 */		
		protected function update(event:Event):void
		{
			// 更新白球位置
			updateCueBall();
			// 更新球杆位置
			if( isHitting )
				moveCue();
			else
				updateCue();
			
			// 检测球杆是否碰到球
			var distance:Number = Point.distance( new Point( cue.x, cue.y ),
				new Point( cueBall.x, cueBall.y ) );
			// 如果正在击球,而且打到了白球
			if( isHitting && distance < 170 )
			{
				// 切换是否正在击球
				isHitting = false;
				// 隐藏球杆
				cue.visible = false;
				// 球杆速度给白球
				vx = vxCue;
				vy = vyCue;
			}
			
		}		
		/**
		 * 更新球杆位置
		 * 
		 */		
		private function updateCue():void
		{			
			// 指向白球
			var dy:Number = cueBall.y - stage.mouseY;
			var dx:Number = cueBall.x - stage.mouseX;
			var radian:Number = Math.atan2( dy, dx );
			var angle:Number = radian * 180 / Math.PI;
			cue.rotation = angle;
			// 限制范围
			var distance:Number = Point.distance( new Point( stage.mouseX, stage.mouseY ),
				new Point( cueBall.x, cueBall.y ) );
			
			if( distance < CUE_BALL_RADIUS +  CUE_WIDTH ) //cueBall.width / 2 + cue.width / 2
			{
				distance = CUE_BALL_RADIUS +  CUE_WIDTH;
			}
			cue.x = cueBall.x + distance * Math.cos(radian + Math.PI );
			cue.y = cueBall.y + distance * Math.sin(radian + Math.PI );
		}
		/**
		 * 更新白球位置
		 * 
		 */		
		private function updateCueBall():void
		{
			// 摩擦力
			vx *= friction;
			vy *= friction;
			// 速度太小时归零
			if( Math.sqrt( vx * vx + vy * vy ) < 0.05 )
			{
				// 白球停止运动
				vx = vy = 0;
				// 显示球杆
				cue.visible = true;
			}
			// 运动
			cueBall.x += vx;
			cueBall.y += vy;
			// 边界检测
			if( cueBall.x < CUE_BALL_RADIUS )
			{
				cueBall.x = CUE_BALL_RADIUS;
				vx *= -1;
			}else if( cueBall.x > stage.stageWidth - CUE_BALL_RADIUS )
			{
				cueBall.x = stage.stageWidth - CUE_BALL_RADIUS;
				vx *= -1;
			}
			
			if( cueBall.y < CUE_BALL_RADIUS )
			{
				cueBall.y = CUE_BALL_RADIUS;
				vy *= -1;
			}else if( cueBall.y > stage.stageHeight- CUE_BALL_RADIUS )
			{
				cueBall.y = stage.stageHeight - CUE_BALL_RADIUS;
				vy *= -1;
			}
		}		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	}
}